﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Configuration;
using System.Data;
using System.Linq;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Xml.Linq;
using Maticsoft.Common;
using System.Text;

namespace Lawyer.Web
{
    public partial class modifyfile : System.Web.UI.Page
    {
        Lawyer.BLL.UserInfo userBll = new Lawyer.BLL.UserInfo();
        Lawyer.BLL.ArchiveInfo archiveBll = new Lawyer.BLL.ArchiveInfo();
        public string filePath = "";
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Request.Params["id"] != null && Request.Params["id"].Trim() != "")
                {
                    string FileID = Request.Params["id"];
                    ShowInfo(FileID);
                    ArchiveDataBind();
                }
            }
        }

        public void ArchiveDataBind()
        {
            List<Model.ArchiveInfo> archiveList = new List<Lawyer.Model.ArchiveInfo>();
            Model.UserInfo userinfo = userBll.GetModel(Session["UserName"].ToString());
            string strWhere = "LawyerID = '" + userinfo.UserID+"'";
            DataSet ds = archiveBll.GetList(strWhere);
            DataTable dt = ds.Tables[0];
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                Model.ArchiveInfo archive = new Lawyer.Model.ArchiveInfo();
                archive.ArchiveID = dt.Rows[i][0].ToString();
                archive.Title = dt.Rows[i][1].ToString();
                archiveList.Add(archive);
            }

            ArchiveList.DataSource = archiveList;
            ArchiveList.DataTextField = "Title";
            ArchiveList.DataValueField = "ArchiveID";
            ArchiveList.DataBind();
 
        }

        private void ShowInfo(string FileID)
        {
            Lawyer.BLL.FileInfo bll = new Lawyer.BLL.FileInfo();
            Lawyer.Model.FileInfo model = bll.GetModel(FileID);
            this.txtFileID.Value = model.FileID;
            this.txtFileName.Value = model.FileName;
            this.txtFileNote.Text = model.FileNote;
            filePath = model.FileURL;
            this.ArchiveList.Value = model.ArchiveID;
            if (model.IsArchive == 1)
            {
                this.checkArchive.Checked = true;
            }
            else
            {
                this.checkArchive.Checked = false;
            }
        }

        protected void btnOK_Click(object sender, EventArgs e)
        {
            string strErr = "";

            if (this.txtFileName.Value.Trim().Length == 0)
            {
                strErr += "file name can't be empty!";
            }
            if (this.txtFileNote.Text.Trim().Length == 0)
            {
                strErr += "file note can't be empty";
            }

            if (strErr != "")
            {
                MessageBox.Show(this, strErr);
                return;
            }
            string FileID = Guid.NewGuid().ToString();
            string FileName = this.txtFileName.Value;
            string FileNote = this.txtFileNote.Text;
            string FileURL = GetContractURL();
            string LawyerID = "binglian";
            string ArchiveID = ArchiveList.Items[ArchiveList.SelectedIndex].Value.ToString();
            int IsArchive = 0;
            if (checkArchive.Checked)
            {
                IsArchive = 1;
            }
            string CreatedBy = "binglian";
            DateTime CreatedDate = System.DateTime.Now;

            Lawyer.Model.FileInfo model = new Lawyer.Model.FileInfo();
            model.FileID = FileID;
            model.FileName = FileName;
            model.FileNote = FileNote;
            model.FileURL = FileURL;
            //model.LawyerID = Session["lawyerID"].ToString();
            model.LawyerID = LawyerID;
            model.ArchiveID = ArchiveID;
            model.IsArchive = IsArchive;
            model.CreatedBy = CreatedBy;
            model.CreatedDate = CreatedDate;

            Lawyer.BLL.FileInfo bll = new Lawyer.BLL.FileInfo();
            bll.Update(model);
            System.IO.FileInfo file = new System.IO.FileInfo(FileURL);
            if (file.Exists)
            {
                file.Delete();
            }
            Maticsoft.Common.MessageBox.ShowAndRedirect(this, "保存成功！", "file.aspx");
        }

        private string GetContractURL()
        {
            string url = "/files/";
            if (fileUpload.HasFile)
            {
                try
                {
                    StringBuilder strBuilder = new StringBuilder();
                    string datetime = System.DateTime.Now.ToFileTime().ToString();
                    string fileName = fileUpload.FileName.Substring(0, fileUpload.FileName.LastIndexOf("."));
                    string fileExt = fileUpload.FileName.Replace(fileName, "");

                    strBuilder.Append(fileName);
                    strBuilder.Append(datetime);
                    strBuilder.Append(fileExt);

                    url = Server.MapPath(url) + strBuilder;
                    // 保存文件
                    fileUpload.SaveAs(url);
                    // 显示文件信息
                }
                catch (Exception ex)
                {
                    url = ex.Message;
                }
            }
            else
            {
                url = "";
            }
            return url;
        }
    }
}
